<?php

/**
 * @author Thomas Champagne
 *
 */
class SportTrackFitDataParser extends AbstractDataParser implements IFitLogDataParser {

	function __construct() {

	}

	/* (non-PHPdoc)
	 * @see IFitLogDataParser::parse($sourceFile)
	 */
	public function parse($sourceFile) {

		$this->source = $sourceFile;

		$this->xml = simplexml_load_file($sourceFile);

		$this->parsingFitTrack();

	}

	private function parsingFitTrack() {

		// Basic infos
		$totalTime = (int) $this->xml->AthleteLog->Activity->Duration->attributes()->TotalSeconds;
		$totalMeters = (int) $this->xml->AthleteLog->Activity->Distance->attributes()->TotalMeters;
		$averageBPM = (int) $this->xml->AthleteLog->Activity->HeartRate->attributes()->AverageBPM;
		$maximumBPM = (int) $this->xml->AthleteLog->Activity->HeartRate->attributes()->MaximumBPM;
		$averageRPM = (int) $this->xml->AthleteLog->Activity->Cadence->attributes()->AverageRPM;
		$maximumRPM = (int) $this->xml->AthleteLog->Activity->Cadence->attributes()->MaximumRPM;
		$totalCal = (int) $this->xml->AthleteLog->Activity->Calories->attributes()->TotalCal;
		$startTime = (string) $this->xml->AthleteLog->Activity->attributes()->StartTime;

		$startTimeTS = (new DateTime($startTime))->getTimestamp();

// 		$startTimeTS = date('y-m-d H:i',$startTimeTS);//(new DateTime())->setTimestamp($startTimeTS)->format('Y-m-d\TH:i:s\Z');;

		// Parsing track
		$xmlTrackPts = $this->xml->AthleteLog->Activity->Track->pt;

		$fitTrackData = Array();
		$speedData = Array();

		$previousFitPt = null;

		foreach ($xmlTrackPts as $pt) {

			$elapsedSeconds = (int) $pt->attributes()->tm;
			$traveledMeters = (int) $pt->attributes()->dist;
			$currentHr = (int) $pt->attributes()->hr;
			$currentCad = (int) $pt->attributes()->cadence;
			$currentTimeTS = $startTimeTS + $elapsedSeconds;
			$currentTime = date('Y-m-d\TH:i:s\Z', $currentTimeTS); // format 2013-07-31T19:14:00Z

			// Finding current speed at point
			$currentSpeedKph = 0;
			$distanceObservedMeters = 0;
			$accel = 0;
			if ($previousFitPt != null) {
				// find cur speed
				$distanceObservedMeters = $traveledMeters - $previousFitPt["traveledDistance"];
				$timeObservedSeconds = $elapsedSeconds - $previousFitPt["elapsedSeconds"];
				$currentSpeedKph = $distanceObservedMeters / $timeObservedSeconds * 3600 / 1000;
				$accel = ($currentSpeedKph - $previousFitPt["currentSpeedKph"]) / $timeObservedSeconds;
			}

			$fitPoint = Array("elapsedSeconds" => $elapsedSeconds, "currentTime" => $currentTime, "currentTimeTS" => $currentTimeTS, "deltaDistance" => $distanceObservedMeters, "traveledDistance" => $traveledMeters, "currentHr" => $currentHr, "currentCad" => $currentCad, "currentSpeedKph" => $currentSpeedKph,  "acceleration" => $accel);

			// Pushing data to arrays
			array_push($speedData, $currentSpeedKph);
			$fitTrackData["$currentTimeTS"] = $fitPoint;
			$fitTrackData[null] = $fitPoint;

			// Store point to calcute speed next loop
			$previousFitPt = $fitPoint;
		}

		// Stats
		$maxSpeed = max($speedData);
		$averageSpeed = array_sum($speedData) / count($speedData);

		$speedDataNotNull = array();
		// Compute average moveing speed (remove 0 speeds)
		foreach ($speedData as $speed) {
			//unset($speedData[$key]);
			if ($speed != 0) {
				array_push($speedDataNotNull, $speed);
			}

		}

		$averageMovingSpeed = array_sum($speedDataNotNull) / count($speedDataNotNull);

		$basicInfos = Array("startTime" => $startTime, "startTimeTS" => $startTimeTS, "totalTimeSeconds" => $totalTime, "totalMeters" => $totalMeters, "maxSpeed" => $maxSpeed, "avgSpeed" => $averageSpeed, "averageMovingSpeed" => $averageMovingSpeed, "averageBPM" => $averageBPM,
				"maximumBPM" => $maximumBPM, "averageCad" => $averageRPM, "maximumCad" => $maximumRPM, "totalCalories" => $totalCal);

		$this->data = Array("fitDataInfos" => $basicInfos, "fitTrackData" => $fitTrackData);

	}

	/* (non-PHPdoc)
	 * @see IFitLogDataParser::getFitData()
	 */
	public function getFitData() {
		return $this->data;
	}

}
